package com.bondar.hashset;

import java.util.Collection;
import java.util.Iterator;

import com.bondar.hashmap.MyHashMap;
import com.bondar.hashmap.MyHashMap.SimpleEntry;
import com.bondar.interfaces.Set;
import com.bondar.interfaces.MyMap.Entry;

public class MyHashSet<T> implements Set<T> {
	
	private MyHashMap<T> map;
	private static final Object EMPTY_VAL = new Object();
	
	public MyHashSet() {
		map = new MyHashMap<T>();
	}
	
	public MyHashSet(int capacity) {
		map = new MyHashMap<T>(capacity);
	}
	
	public MyHashSet(int capacity, double loadFactor) {
		map = new MyHashMap<T>(capacity, loadFactor);
	}

	@Override
	public boolean contains(Collection<T> elements) {
		
		for (T element : elements) {
			if ( !contains(element) ) {
				return false;
			}
		}
		
		return true;
	}

	@Override
	public int size() {
		return map.size();
	}

	@Override
	public boolean isEmpty() {
		return size() == 0;
	}

	@Override
	public Iterator<T> iterator() {
		
		return new Iterator<T>() {
			
			Iterator<Entry<T>> it = map.entryIterator();

			@Override
			public boolean hasNext() {
				return it.hasNext();
			}

			@Override
			public T next() {
				SimpleEntry<T> entry = (SimpleEntry<T>) it.next();
				return entry.getKey();
			}

			@Override
			public void remove() {
				it.remove();
			}
		};
	}

	@Override
	public boolean contains(T element) {
		return map.containsKey(element);
	}

	@Override
	public void add(T element) {
		if (!map.containsKey(element)) {
			map.put(element, EMPTY_VAL);
		}
	}

	@Override
	public boolean remove(T element) {
		return map.remove(element) == EMPTY_VAL;
	}

	@Override
	public void clear() {
		map.clear();
	}

}
